<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>AddStudent</title>
    <style>
        .container{
            width: 50%;
            margin: 0 auto;
            text-align: center;
        }
        .title{
            margin: 20px 0;
            font-size: x-large;
        }
        .stu{
            margin-top: 20px;
            border: 1px solid #e9e9e9;
            padding: 55px 0;
            box-shadow: 0 0 100px 30px #D9D9D9;
            border-radius: 15px;
        }
        .stuTable{
            margin: 0 auto;
        }
        .sub{
            margin: 20px 0 0 0;
        }
        .sub .submit{
            margin-right: 10px;
        }
         .sub .back{
            margin-left: 10px;
        }
        #sdept{
            height: 21.3px;
            width: 166px;
        }
        .pro{
            color: red;
        }
    </style>
</head>
<body>
<?php
/*
 * 初始化数据
 */
$idPromt = "";
$namePromt = "";
$passwordPromt = "";
$birthdayPromt = "";
$phonePromt = "";
$emailPromt = "";
$sdeptPromt = "";
if (isset($_REQUEST['add'])){    //判断表单是否提交
    $isTrue = 1;
    /*
     * 获取表单数据
     */
    $id = $_REQUEST['sno'];
    $name = $_REQUEST['name'];
    $password = $_REQUEST['password'];
    $sex = $_REQUEST['sex'];
    $birthday = $_REQUEST['birthday'];
    $sdept = $_REQUEST['sdept'];
    $phone = $_REQUEST['phone'];
    $email = $_REQUEST['email'];

    /*
     * 上传图片
     */
    if($_FILES["image"]["error"]>0){
        echo "图片上传错误：: " .$_FILES["image"]["error"] . "<br>";   //图片上传错误时提示
    }else{
        $fileName = $_FILES['image']['name'];    //获取上传图片的文件名
        $fileName = iconv('UTF-8','GB2312',$fileName);  //设置文件编码
        date_default_timezone_set("Asia/shanghai");   //设置时区
        $mytime = date("YmdHis");   //设置时间戳日期转换
        $tagarr = explode(".",$_FILES['image']['name']);   //拆分文件名
        $myextension = end($tagarr);    //获取扩展名
        $target_dir = "./images/";   //保存到本地目录
        $target_file = $target_dir.$mytime.'.'.$myextension;
        $fileName = $mytime.'.'.$myextension;    //拼接后的文件名
        $temSrc = $_FILES['image']['tmp_name'];    //文件的临时存放目录
        move_uploaded_file($temSrc, $target_file);   //将文件上传到目标目录
    }

    /*
     * 表单验证
     */
    if ($id == ""){
        $idPromt = "请输入id";
        $isTrue = 0;
    } elseif(preg_match("/^\d{1,6}$/",$id)==0){
        $idPromt = "id长度为1-6位数字";
        $isTrue = 0;
    }
    if ($name == ""){
        $namePromt = "请输入name";
        $isTrue = 0;
    }
    if ($password == ""){
        $passwordPromt = "请输入密码";
        $isTrue = 0;
    }
    if ($birthday == ""){
        $birthdayPromt = "请输入生日";
        $isTrue = 0;
    }elseif(preg_match(" /^(19|20)\d{2}-(1[0-2]|0?[1-9])-(0?[1-9]|[1-2][0-9]|3[0-1])$/",$birthday)==0){
        $birthdayPromt = "生日格式为：yyyy-mm-dd";
        $isTrue = 0;
    }
    if ($sdept == ""){
        $sdeptPromt = "请选择部门！！！";
        $isTrue = 0;
    }
    if ($phone == ""){
        $phonePromt = "请输入手机号码";
        $isTrue = 0;
    }elseif(preg_match("/^1\d{10}$/",$phone)==0){
        $phonePromt = "手机号码为以1开头的11位数字";
        $isTrue = 0;
    }
    if ($email == ""){
        $emailPromt = "请输入邮箱";
        $isTrue = 0;
    }elseif(preg_match("/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/",$email)==0){
        $emailPromt = "请输入正确的邮箱格式";
        $isTrue = 0;
    }

    if($isTrue){    //判断表单是否验证成功
        include "./conn.php";
        $sql = "insert into stu values($id, '$name', '$password', '$sex', '$birthday', '$sdept', $phone,'$email','$fileName')";
        $result  = mysqli_query($con,$sql);
        if($result) echo "<script language='JavaScript'>alert('插入成功')</script>";  //插入成功提示
        else echo "插入失败！";   //插入失败提示
    }

}
?>
    <div class="container">
        <div class="title">添加学生信息</div>
        <form action="" method="post" name="addStu" class="stu" enctype="multipart/form-data">
            <table class="stuTable">
                <tr>
                    <td><span>学号:</span></td>
                    <td><input type="text" name="sno"><span></span></td>
                    <td class="pro"><?php echo $idPromt?></td>
                </tr>
                <tr>
                    <td><span>姓名:</span></td>
                    <td><input type="text" name="name"></td>
                    <td class="pro"><?php echo $namePromt?></td>
                </tr>
                <tr>
                    <td><span>密码:</span></td>
                    <td><input type="password" name="password"></td>
                    <td class="pro"><?php echo $passwordPromt?></td>
                </tr>
                <tr>
                    <td><span>性别:</span></td>
                    <td>男<input type="radio" name="sex" value="男" checked>
                        女<input type="radio" name="sex" value="女"> </td>
                </tr>
                <tr>
                    <td><span>生日:</span></td>
                    <td><input type="text" name="birthday"></td>
                    <td class="pro"><?php echo $birthdayPromt?></td>
                </tr>
                <tr>
                    <td><span>部门:</span></td>
                    <td><select name="sdept" id="sdept" >
                            <option value="" selected>请选择部门</option>
                            <option value="信息工程学院">信息工程学院</option>
                            <option value="彩虹鱼学院">彩虹鱼学院</option>
                            <option value="汽车学院">汽车学院</option>
                            <option value="达利女装学院">达利女装学院</option>
                        </select>
                    </td>
                    <td class="pro"><?php echo $sdeptPromt?></td>
                </tr>
                <tr>
                    <td><span>手机:</span></td>
                    <td><input type="text" name="phone"></td>
                    <td class="pro"><?php echo $phonePromt?></td>
                </tr>
                <tr>
                    <td><span>Email:</span></td>
                    <td><input type="text" name="email"></td>
                    <td class="pro"><?php echo $emailPromt?></td>
                </tr>
                <tr>
                    <td><span>图片:</span></td>
                    <td><input type="file" name="image" style="width: 166.3px"></td>
                </tr>
            </table>
            <div class="sub">
                <input type="submit" class="submit" name="add" value="添加">
                <input type="button" class="back" onclick="window.location.href='stuList.php'" value="返回">
            </div>
        </form>
    </div>
</body>
</html>